<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/31
 * Time: 17:03
 */

namespace common\sources\read\businessdepositrecord;


use common\models\craftsman\BusinessDepositRecord;
use common\models\craftsman\BusinessRechargeRecord;

class BusinessDepositRecordAccess implements IBusinessDepositRecordAccess
{
    public function getCount($data)
    {
        $where = $this->getWhere($data);
        return BusinessDepositRecord::find()
                ->where($where)
                ->count();
    }

    public function getList($data)
    {
        $where = $this->getWhere($data);
        return BusinessDepositRecord::find()
                ->select("task_id,task_order_num,expend,income,balance,remark,create_time")
                ->where($where)
                ->orderBy("id desc")
                ->offset(($data['page_num'] -1) * $data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
    }

    private function getWhere($data)
    {
        $sql = " bus_id = ".$data['bus_id'];
        if(!empty($data['task_id']) && isset($data['task_id']))
        {
            $sql .= " AND task_id = '".$data['task_id']."'";
        }
        if(!empty($data['start_time']) && isset($data['start_time']))
        {
            $sql .= " AND create_time >= ".$data['start_time'];
        }
        if(!empty($data['end_time']) && isset($data['end_time']))
        {
            $sql .= " AND create_time <= ".$data['end_time'];
        }
        return $sql;
    }

    public function getRechargeCount($data)
    {
        return BusinessDepositRecord::find()
            ->where("bus_id = ".$data['bus_id']." AND type =1")
            ->count();
    }

    public function getRechargeList($data)
    {
        return BusinessRechargeRecord::find()
                ->select("amount,shop_name,create_time")
                ->where("bus_id =".$data['bus_id']." AND pay_type = 5")
                ->orderBy("id desc")
                ->offset(($data['page_num']-1) * $data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
//        return BusinessDepositRecord::find()
//            ->select("income,remark,create_time,shop_name")
//            ->where("bus_id = ".$data['bus_id']." AND type =1")
//            ->orderBy("id desc")
//            ->offset(($data['page_num'] -1) * $data['limit'])
//            ->limit($data['limit'])
//            ->asArray()
//            ->all();
    }

}